package com.ushine.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushine.web.base.model.system.PostDO;
import com.ushine.web.base.service.IPostService;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.component.page.PageResultVO;
import com.ushine.web.log.annotation.SystemControllerLog;
import com.ushine.web.log.constant.LogConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Author: yls
 * @Date: 2022/03/01 10:47
 * @Description: 岗位管理
 * @Version 1.0
 */
@RestController
@RequestMapping("system/post")
@Api(tags = "基础模块-岗位管理")
@Validated
public class PostController {

    @Resource
    private IPostService postService;

    @SystemControllerLog(operation = LogConstant.CREATE)
    @ApiOperation(value = "添加岗位")
    @PostMapping("insertPost")
    public HttpResponseVO<Object> insertPost(@Validated @ApiParam(value = "岗位实体", required = true) @RequestBody PostDO post) {
        Optional<PostDO> postByPostName = postService.getPostByPostName(post.getPostName());
        if (postByPostName.isPresent()) {
            return new HttpResponseVO<>().error("添加失败，岗位名称已存在！");
        }
        Boolean flag = postService.insertPost(post);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("添加成功！");
        } else {
            return new HttpResponseVO<>().error("添加失败！");
        }
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @ApiOperation(value = "分页查询岗位")
    @PostMapping("listPostByPage")
    public HttpResponseVO<PageResultVO<PostDO>> listPostByPage(@ApiParam(value = "查询岗位条件", required = true) @RequestBody PageRequestQuery pageRequestQuery) {
        Page<PostDO> orgPage = postService.listPostsByPage(pageRequestQuery);
        HttpResponseVO<PageResultVO<PostDO>> responseVO = new HttpResponseVO<>();
        return responseVO.ok("查询成功！", orgPage);
    }
}
